package com.atguigu.flink.chapter06.join;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.chapter05.function.WaterSensorMapFunction;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

public class IntervalJoinDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        KeyedStream<WaterSensor, String> s1 = env.socketTextStream("hadoop102", 8888)
                .map(new WaterSensorMapFunction())
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forMonotonousTimestamps()
                                .withTimestampAssigner((ws, ts) -> ws.getTs())
                )
                .keyBy(WaterSensor::getId);

        KeyedStream<WaterSensor, String> s2 = env.socketTextStream("hadoop102", 9999)
                .map(new WaterSensorMapFunction())
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forMonotonousTimestamps()
                                .withTimestampAssigner((ws, ts) -> ws.getTs())
                )
                .keyBy(WaterSensor::getId);

        s1.intervalJoin(s2)
                .between(Time.seconds(-5),Time.seconds(5))
                //.lowerBoundExclusive()
                //.upperBoundExclusive()
                .process(new ProcessJoinFunction<WaterSensor, WaterSensor, String>() {
                    @Override
                    public void processElement(WaterSensor left,
                                               WaterSensor right,
                                               Context ctx,
                                               Collector<String> out) throws Exception {
                        out.collect(left + "<>" + right);
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



//public class IntervalJoinDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port", 2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        KeyedStream<WaterSensor, String> s1 = env.socketTextStream("hadoop102", 8888)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//                )
//                .keyBy(WaterSensor::getId);
//
//        KeyedStream<WaterSensor, String> s2 = env.socketTextStream("hadoop102", 9999)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//                )
//                .keyBy(WaterSensor::getId);
//
//
//        s1.intervalJoin(s2)
//                .between(Time.seconds(-5),Time.seconds(5))
//                //.lowerBoundExclusive()
//                //.upperBoundExclusive()
//                .process(new ProcessJoinFunction<WaterSensor, WaterSensor, String>() {
//                    @Override
//                    public void processElement(WaterSensor left,
//                                               WaterSensor right,
//                                               Context ctx,
//                                               Collector<String> out) throws Exception {
//                        out.collect(left + "<>" + right);
//                    }
//                })
//                .print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}

//public class IntervalJoinDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port", 2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        KeyedStream<WaterSensor, String> s1 = env.socketTextStream("hadoop102", 8888)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//                )
//                .keyBy(WaterSensor::getId);
//
//
//        KeyedStream<WaterSensor, String> s2 = env.socketTextStream("hadoop102", 9999)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//                )
//                .keyBy(WaterSensor::getId);
//
//        s1.intervalJoin(s2)
//                .between(Time.seconds(-5),Time.seconds(5))
//                //.lowerBoundExclusive()
//                //.upperBoundExclusive()
//                .process(new ProcessJoinFunction<WaterSensor, WaterSensor, String>() {
//                    @Override
//                    public void processElement(WaterSensor left,
//                                               WaterSensor right,
//                                               Context ctx,
//                                               Collector<String> out) throws Exception {
//                        out.collect(left + "<>" + right);
//                    }
//                })
//                .print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}



//public class IntervalJoinDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port", 2000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        KeyedStream<WaterSensor, String> s1 = env.socketTextStream("hadoop102", 8888)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//
//                )
//                .keyBy(WaterSensor::getId);
//        KeyedStream<WaterSensor, String> s2 = env.socketTextStream("hadoop102", 9999)
//                .map(new WaterSensorMapFunction())
//                .assignTimestampsAndWatermarks(
//                        WatermarkStrategy
//                                .<WaterSensor>forMonotonousTimestamps()
//                                .withTimestampAssigner((ws, ts) -> ws.getTs())
//
//                )
//                .keyBy(WaterSensor::getId);
//
//
//        s1.intervalJoin(s2)
//                .between(Time.seconds(-5),Time.seconds(5))
//                .lowerBoundExclusive()
//                .upperBoundExclusive()
//                .process(new ProcessJoinFunction<WaterSensor, WaterSensor, String>() {
//                    @Override
//                    public void processElement(WaterSensor left,
//                                               WaterSensor right,
//                                               Context ctx,
//                                               Collector<String> out) throws Exception {
//                        out.collect(left + "<>" + right);
//                    }
//                })
//                .print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
